package com.wfh.mianshiji.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfh.mianshiji.common.ErrorCode;
import com.wfh.mianshiji.constant.UserConstant;
import com.wfh.mianshiji.exception.BusinessException;
import com.wfh.mianshiji.model.dto.conversion.ConversionQueryRequest;
import com.wfh.mianshiji.model.entity.Conversion;
import com.wfh.mianshiji.model.entity.Interview;
import com.wfh.mianshiji.model.vo.ConversionVo;
import com.wfh.mianshiji.model.vo.UserVo;
import com.wfh.mianshiji.service.ConversionService;
import com.wfh.mianshiji.mapper.ConversionMapper;
import com.wfh.mianshiji.service.InterviewService;
import com.wfh.mianshiji.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author fenghuanwang
* @description 针对表【conversion(消息对话表)】的数据库操作Service实现
* @createDate 2025-10-30 15:36:18
*/
@Service
public class ConversionServiceImpl extends ServiceImpl<ConversionMapper, Conversion>
    implements ConversionService{

    @Resource
    private UserService userService;

    @Resource
    private InterviewService interviewService;

    /**
     * 获取查询包装类
     *
     * @param conversionQueryRequest
     * @return
     */
    private QueryWrapper<Conversion> getQueryMapper(ConversionQueryRequest conversionQueryRequest) {
        QueryWrapper<Conversion> query = Wrappers.query(Conversion.class);
        Long userId = conversionQueryRequest.getUserId();
        Long interviewId = conversionQueryRequest.getInterviewId();
        String messageType = conversionQueryRequest.getMessageType();
        String message = conversionQueryRequest.getMessage();
        Date date = conversionQueryRequest.getDate();

        query.eq("userId", userId)
                .eq("interviewId", interviewId)
                .eq("messageType", messageType)
                .like(StringUtils.isNotEmpty(message), "message", message);

        // 游标查询
        if (date != null){
            query.lt("createTime", date);
        }
        // 排序,按照创建时间降序排序
        query.orderBy(true, false, "createTime");
        return query;
    }

    /**
     * 分页友表查询对话历史
     *
     * @param conversionQueryRequest
     * @param request
     * @return
     */
    @Override
    public Page<ConversionVo> getConversionPage(ConversionQueryRequest conversionQueryRequest, HttpServletRequest request) {
        UserVo currentUser = userService.getCurrentUser(request);
        Long userId = conversionQueryRequest.getUserId();
        long current = conversionQueryRequest.getCurrent();
        long pageSize = conversionQueryRequest.getPageSize();
        boolean equals = currentUser.getUserRole().equals(UserConstant.ADMIN_ROLE);
        if (!userId.equals(currentUser.getId()) || !equals){
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "只能查询自己的对话历史");
        }
        Interview interview = interviewService.getById(conversionQueryRequest.getInterviewId());
        if (interview == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "面试场次不存在");
        }
        // 获取查询包装器
        QueryWrapper<Conversion> queryMapper = this.getQueryMapper(conversionQueryRequest);
        Page<Conversion> page = this.page(Page.of(current, pageSize), queryMapper);
        List<ConversionVo> list = page.getRecords().stream().map(conversion -> BeanUtil.copyProperties(conversion, ConversionVo.class)).toList();
        Page<ConversionVo> res = new Page<>(current, pageSize);
        res.setRecords(list);
        return res;
    }
}




